import axios from "./axios.js";
import { ElMessage } from "element-plus";
import { IsDevelopment } from "@/utils/configure";

// 请求拦截器
axios.interceptors.request.use(config => {
  return config;
});

// 响应拦截器
axios.interceptors.response.use(
  response => {
    const { data } = response;
    return data;
  },
  error => {
    const { status, data } = error;
    data && data?.msg && ElMessage.error(data?.msg);
    switch (status) {
      case 400:
        return Promise.reject(error);
      case 401:
        window.location.href = IsDevelopment ? "/auth/login" : "#/auth/login";
        return Promise.reject(error);
      case 403:
        return Promise.reject(error);
      case 404:
        return Promise.reject(error);
      case 500:
        return Promise.reject(error);
      default:
        return Promise.reject(error);
    }
  }
);

export default axios;
